package com.bsoft.controller;

import com.alibaba.fastjson.JSON;
import com.bsoft.service.IRestHighLevelClientService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@Slf4j
@RestController
public class EsController {

    @Autowired
    IRestHighLevelClientService iRestHighLevelClientService;

    @GetMapping("/createIndex")
    //创建索引
    public void createIndex() throws IOException {
        CreateIndexResponse index = iRestHighLevelClientService.createIndex();
        System.out.println("新建的索引是" + index.index());

    }

    @GetMapping("/deleteIndex")
    //删除索引
    public void deleteIndex() throws IOException {
        AcknowledgedResponse acknowledgedResponse = iRestHighLevelClientService.deleteIndex();
        System.out.println("删除索引是否成功" + acknowledgedResponse.isAcknowledged());
        log.info(JSON.toJSONString(acknowledgedResponse));
    }

    // 判断所有是否存在
    @GetMapping("/existIndex")
    public void existIndex() throws IOException {
        boolean b = iRestHighLevelClientService.existIndex();
        System.out.println(b);


    }
}
